package com.auxgroup.smarthome.inner.service;

import com.auxgroup.bridge.business.inner.dto.BusiAppIndexPageDto;
import com.auxgroup.bridge.business.inner.vo.AppIndexPageVo;
import com.auxgroup.bridge.business.inner.vo.CurrentAppIndexPageVo;
import com.auxgroup.smarthome.BeanUtils;
import com.auxgroup.smarthome.appconst.BusinessConstant;
import com.auxgroup.smarthome.inner.entity.AppIndexPageEntity;
import com.auxgroup.smarthome.inner.repo.AppIndexPageEntityRepo;
import com.auxgroup.smarthome.syscode.BusinessCode;
import com.auxgroup.smarthome.syscode.Syscode;
import com.auxgroup.smarthome.utils.PageUtils;
import com.auxgroup.smarthome.web.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Created by niuGuangzhe on 2017/8/2.
 */
@Service
public class AppIndexPageService {
    @Autowired
    private AppIndexPageEntityRepo appIndexPageEntityRepo;

    /**
     * 保存app首页管理
     * @param busiAppIndexPageDto
     * @return
     */
    @Transactional
    public ApiResponse<AppIndexPageVo> saveAppIndexPage(BusiAppIndexPageDto busiAppIndexPageDto) {
        AppIndexPageEntity appIndexPageEntity = appIndexPageEntityRepo.findByStatus(BusinessConstant.APP_INDEX_PAGE_SAVE.getValue());
        if(null == appIndexPageEntity) {
            appIndexPageEntity = new AppIndexPageEntity();
        }
        appIndexPageEntity.createIndexVersion(busiAppIndexPageDto.getZipUri(), busiAppIndexPageDto.getHtmlUri());
        appIndexPageEntity = appIndexPageEntityRepo.save(appIndexPageEntity);
        return ApiResponse.ok(BeanUtils.copyAttrs(new AppIndexPageVo(), appIndexPageEntity));
    }


    /**
     * 发布app首页管理
     *
     * @param indexPageId 主键
     * @return
     */
    @Transactional(readOnly = false)
    public ApiResponse publishAppIndexPage(String indexPageId, String releseUser) {
        // 发布app首页版本
        AppIndexPageEntity willPublishEntity = appIndexPageEntityRepo.findByIndexPageId(indexPageId);
        if (willPublishEntity == null) {
            ApiResponse.prompt(Syscode.APP_INDEX_PAGE_PUBLISH_VERSION_NOT_EXIST);
        }

        // 已发布版本变为历史
        AppIndexPageEntity alreadyPublishEntity = appIndexPageEntityRepo.findByStatus(BusinessConstant.APP_INDEX_PAGE_RELEASE.getValue());
        if (alreadyPublishEntity != null) {
            alreadyPublishEntity.recordIndexVersion();
            appIndexPageEntityRepo.save(alreadyPublishEntity);
        }

        willPublishEntity.releaseIndexVersion(releseUser);
        appIndexPageEntityRepo.save(willPublishEntity);
        return ApiResponse.prompt(BusinessCode.SC_OK);
    }


    /**
     * 获取app首页管理最新5条数据
     *
     * @return
     */
    public ApiResponse<List<AppIndexPageVo>> getAppIndexPageList() {
        //按照创建时间降序排列
        Sort sort = new Sort(Sort.Direction.DESC, "createdAt");
        Pageable pageable = new PageRequest(0, 5, sort);

        Page<AppIndexPageEntity> entities = appIndexPageEntityRepo.findAll(pageable);
        Page<AppIndexPageVo> vos = new PageUtils(AppIndexPageVo.class, entities).buildVosPage();
        return ApiResponse.ok(vos.getContent());
    }

    /**
     * 获取最新的发布版本
     */
    public ApiResponse<AppIndexPageVo> getlatestIndexPage() {
        AppIndexPageEntity entity = appIndexPageEntityRepo.findByStatus(BusinessConstant.APP_INDEX_PAGE_RELEASE.getValue());
        if (entity == null) {
            ApiResponse.prompt(Syscode.APP_INDEX_PAGE_PUBLISH_VERSION_NOT_EXIST);
        }
        return ApiResponse.ok(BeanUtils.copyAttrs(new AppIndexPageVo(), entity));
    }

    /**
     * 获取最新的版本
     */
    public ApiResponse<CurrentAppIndexPageVo> getlatestIndexPageVersion() {
        AppIndexPageEntity save = appIndexPageEntityRepo.findByStatus(BusinessConstant.APP_INDEX_PAGE_SAVE.getValue());
        AppIndexPageEntity release = appIndexPageEntityRepo.findByStatus(BusinessConstant.APP_INDEX_PAGE_RELEASE.getValue());
        CurrentAppIndexPageVo currentAppIndexPageVo = new CurrentAppIndexPageVo();
        if(null != save) {
            currentAppIndexPageVo.setSave(BeanUtils.copyAttrs(new AppIndexPageVo(), save));
        }
        if(null != release) {
            currentAppIndexPageVo.setRelease(BeanUtils.copyAttrs(new AppIndexPageVo(), release));
        }
        return ApiResponse.ok(currentAppIndexPageVo);
    }

}
